package com.shujia.mr.pcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/*
张三,12月3号,20
张三,12月3号,10
李四,12月3号,12
王五,12月3号,10
王五,12月2号
王五,12月2号,30
王二麻,12月2号,0
王二麻,12月2号,,
王二麻,12月2号, ,
 */
public class PcountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //王五,12月3号,10  --> 3
        //王五,12月2号     --> 2
        //王二麻,12月2号,, --> 4
        //王二麻,12月2号, , -->4
        String line = value.toString();
        String[] strings = line.split(",", -1);
        switch (strings.length) {
            case 3:
                context.write(new Text(strings[0]), new LongWritable(Long.parseLong(strings[2])));
                break;
            case 2:
                context.write(new Text(strings[0]), new LongWritable(0L));
                break;
            case 4:
                if ("".equals(strings[2]) || " ".equals(strings[2])) {
                    strings[2] = "0";
                    context.write(new Text(strings[0]), new LongWritable(Long.parseLong(strings[2])));
                    break;
                } else {
                    context.write(new Text(strings[0]), new LongWritable(Long.parseLong(strings[2])));
                    break;
                }
        }
    }
}
